<?php

namespace App\Http\Controllers\Admin\Meeting;

use App\Handlers\Oss;
use App\Http\Controllers\Admin\AdminController;
use App\Models\Meeting;
use Illuminate\Http\Request;
use App\Api\Exceptions\ApiCommonException;
use Illuminate\Validation\Rule;
use Validator;
use Lang;

class MeetingsController extends AdminController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, Meeting $meeting)
    {
        $per_page = $request->input('per_page', $this->per_page);
        $meeting_name = $request->meeting_name;

        $current_query = $meeting->select('id', 'meeting_name', 'sign_in_start_at', 'sign_in_end_at', 'sign_up_num', 'status', 'created_at');

        if (!empty($meeting_name)) {
            $current_query->where('meeting_name', 'like', '%'.$meeting_name.'%');
        }

        $data = $current_query->orderBy('id', 'DESC')->paginate($per_page);

        return $this->getResponseSuccess($data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, Meeting $meeting)
    {
        // 创建验证规则
        $rules = [
            'meeting_name' => [
                'bail',
                'required',
                'max:20',
                Rule::unique('meetings')->where(function ($query) {
                    $query->whereNull('deleted_at');
                }),
            ],
            'meeting_image' => 'bail|required|',
            'meeting_desc' => 'bail|required|min:10|max:500',
            'sign_in_start_at' => 'bail|required|date_format:Y-m-d H:i:s',
            'sign_in_end_at' => 'bail|required|date_format:Y-m-d H:i:s',
        ];

        // 自定义验证消息
        $messages = [
            'meeting_name.required' => Lang::get('meeting.meeting_name').Lang::get('common.required'),
            'meeting_name.max' => Lang::get('meeting.meeting_name').Lang::get('common.max'),
            'meeting_name.unique' => Lang::get('meeting.meeting_name').Lang::get('common.unique'),
            'meeting_image.required' => Lang::get('meeting.meeting_image').Lang::get('common.required'),
            'meeting_desc.required' => Lang::get('meeting.meeting_desc').Lang::get('common.required'),
            'meeting_desc.min' => Lang::get('meeting.meeting_desc').Lang::get('common.min'),
            'meeting_desc.max' => Lang::get('meeting.meeting_desc').Lang::get('common.max'),
            'sign_in_start_at.required' => Lang::get('meeting.sign_in_start_at').Lang::get('common.required'),
            'sign_in_start_at.date_format' => Lang::get('meeting.sign_in_start_at').Lang::get('common.date_format'),
            'sign_in_end_at.required' => Lang::get('meeting.sign_in_end_at').Lang::get('common.required'),
            'sign_in_end_at.date_format' => Lang::get('meeting.sign_in_end_at').Lang::get('common.date_format'),
        ];

        //开始验证
        $validator = Validator::make($request->all(), $rules, $messages);

        if($validator->fails())
        {
            throw ApiCommonException::requestParamsErr($validator->messages()->first());
        }

        $data = $request->all();
        $data['sign_up_start_at'] = date('Y-m-d H:i:s');
        $data['sign_up_end_at'] = $request->sign_in_start_at;

        $meeting->fill($data);

        $meeting->save();

        return $this->getResponseSuccess();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data = Meeting::select('id', 'meeting_name', 'meeting_image', 'meeting_desc', 'sign_in_start_at', 'sign_in_end_at', 'sign_up_num', 'sign_in_num', 'status', 'created_at')->find($id);

        if (!empty($data)) {
            $data->meeting_image_url = Oss::getPublicObjectURL($data->meeting_image);
        }

        return $this->getResponseSuccess($data);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // 创建验证规则
        $rules = [
            'meeting_name' => [
                'bail',
                'required',
                'max:20',
                Rule::unique('meetings')->ignore($id)->where(function ($query) {
                    $query->whereNull('deleted_at');
                }),
            ],
            'meeting_image' => 'bail|required|',
            'meeting_desc' => 'bail|required|min:10|max:500',
            'sign_in_start_at' => 'required|date_format:Y-m-d H:i:s',
            'sign_in_end_at' => 'required|date_format:Y-m-d H:i:s',
        ];

        // 自定义验证消息
        $messages = [
            'meeting_name.required' => Lang::get('meeting.meeting_name').Lang::get('common.required'),
            'meeting_name.max' => Lang::get('meeting.meeting_name').Lang::get('common.max'),
            'meeting_name.unique' => Lang::get('meeting.meeting_name').Lang::get('common.unique'),
            'meeting_image.required' => Lang::get('meeting.meeting_image').Lang::get('common.required'),
            'meeting_desc.required' => Lang::get('meeting.meeting_desc').Lang::get('common.required'),
            'meeting_desc.min' => Lang::get('meeting.meeting_desc').Lang::get('common.min'),
            'meeting_desc.max' => Lang::get('meeting.meeting_desc').Lang::get('common.max'),
            'sign_in_start_at.required' => Lang::get('meeting.sign_in_start_at').Lang::get('common.required'),
            'sign_in_start_at.date_format' => Lang::get('meeting.sign_in_start_at').Lang::get('common.date_format'),
            'sign_in_end_at.required' => Lang::get('meeting.sign_in_end_at').Lang::get('common.required'),
            'sign_in_end_at.date_format' => Lang::get('meeting.sign_in_end_at').Lang::get('common.date_format'),
        ];

        //开始验证
        $validator = Validator::make($request->all(), $rules, $messages);

        if($validator->fails())
        {
            throw ApiCommonException::requestParamsErr($validator->messages()->first());
        }

        Meeting::findOrFail($id)->update($request->all());

        return $this->getResponseSuccess();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $id = explode(",", $id);

        $r = Meeting::destroy($id);

        if (!$r) {
            throw ApiCommonException::requestParamsErr(Lang::get('common.action_error'));
        }

        return $this->getResponseSuccess();
    }
}
